package com.mrx.pvzcommunity.service;

import com.mrx.pvzcommunity.dao.UserThreadDao;
import com.mrx.pvzcommunity.model.Response;
import com.mrx.pvzcommunity.model.Result;
import com.mrx.pvzcommunity.model.thread.UserThread;
import com.mrx.pvzcommunity.model.user.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserThreadService {
    private final UserThreadDao userThreadDao;

    /**
     * 通过 User 对象查找 帖子
     *
     * @param user 要查找的用户
     * @return 帖子列表
     */
    public Result<List<UserThread>> findUserThreadByUser(User user) {
        if (user == null) {
            return Result.createResult(Response.Code.USER_NOT_LOGGED, Collections.emptyList());
        }
        List<UserThread> res = userThreadDao.findThreadsByUser(user);
        if (res.isEmpty()) {
            return Result.createResult(Response.Code.NOT_FOUND, res);
        }
        return Result.createResult(Response.Code.SUCCESS, res);
    }

    /**
     * 通过 uid 查找帖子
     *
     * @param uid 要查找的 uid
     * @return 帖子列表
     */
    public Result<List<UserThread>> findThreadsByUid(String uid) {
        List<UserThread> res = userThreadDao.findThreadsByUserId(uid);
        if (res.isEmpty()) {
            return Result.createResult(Response.Code.NOT_FOUND, res);
        }
        return Result.createResult(Response.Code.SUCCESS, res);
    }

    /**
     * 通过 tid 查找帖子
     *
     * @param tid 要查找的 tid
     * @return 帖子列表
     */
    public Result<List<UserThread>> findThreadsByTid(String tid) {
        List<UserThread> res = userThreadDao.findThreadsByTid(tid);
        if (res.isEmpty()) {
            return Result.createResult(Response.Code.THREADS_NOT_EXISTS, res);
        }
        return Result.createResult(Response.Code.SUCCESS, res);
    }
}
